package com.edgclub.edg.activity;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.edgclub.edg.R;
import com.edgclub.edg.fragment.FindStarFragment;
import com.edgclub.edg.model.User;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/6/28.
 */
public class EDGStarComponent {
    private Activity mAct;
    private ArrayList<User> stars;
    private LinearLayout viewGroup;
    private int preEnablePositon = 0; // 前一个被选中的点的索引位置 默认情况下为0
    private ViewPager viewPager;

    private ArrayList<Fragment> fragmentList;
    private FragmentManager fm;

    private int page = 1;


    public void init(final Activity mAct, final ViewPager viewPager, LinearLayout viewGroup,FragmentManager fm, ArrayList<User> stars) {
        this.mAct = mAct;
        this.viewPager = viewPager;
        this.viewGroup = viewGroup;
        this.stars = stars;
        this.fm = fm;
        initdata();
        initDot();
    }

    private void initdata() {
        fragmentList = new ArrayList<Fragment>();
        int num=stars.size()%5;
        if(num==0){
            page=stars.size()/5;
        }else{
            page=stars.size()/5+1;
        }
        for(int i = 0;i<page;i++){
            ArrayList<User> tmpList = new ArrayList<>();
            for(int j=i*5;j<stars.size();j++){
                tmpList.add(stars.get(j));
                if(j==i*5+4)break;
            }
            FindStarFragment findStarFragment = FindStarFragment.newInstance(tmpList);
            fragmentList.add(findStarFragment);
        }
        //给ViewPager设置适配器
        viewPager.setAdapter(new MyFragmentPagerAdapter(fm, fragmentList));
        viewPager.setCurrentItem(0);//设置当前显示标签页为第一页
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器


    }

    /**
     * 初始化小圆点
     */
    private void initDot() {
        viewGroup.removeAllViews();
        preEnablePositon = 0;
        LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < page; i++) {
            if(mAct==null)return;
            ImageView dotImageView = new ImageView(mAct);
            dotImageView.setId(i);
            dotImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            dotImageView.setLayoutParams(localLayoutParams);
            dotImageView.setPadding(5, 5, 5, 5);
            dotImageView.setImageResource(R.drawable.gallery_page_indicator_selector);

            if (i == 0) {
                dotImageView.setEnabled(true);//第一次选上第一个
            }else{
                dotImageView.setEnabled(false);
            }
            viewGroup.addView(dotImageView);
        }
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    public class  MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            // 取余后的索引
            int newPositon = position ;

            // 把上一个点设置为被选中
            viewGroup.getChildAt(preEnablePositon).setEnabled(false);

            // 根据索引设置那个点被选中
            viewGroup.getChildAt(newPositon).setEnabled(true);

            preEnablePositon = newPositon;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
